package gateway.route;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PrometheusTextParser {

    // Prometheus 数据示例
    private static final String PROMETHEUS_TEXT = "" +
            "# HELP http_requests_total The total number of HTTP requests.  \n" +
            "# TYPE http_requests_total counter  \n" +
            "http_requests_total{method=\"get\",code=\"200\"} 100  \n" +
            "http_requests_total{method=\"post\",code=\"200\"} 200  \n" +
            "# HELP some_other_metric A help for the other metric.  \n" +
            "# TYPE some_other_metric gauge  \n" +
            "some_other_metric 42  " ;

    public static void main(String[] args) {
        // 使用正则表达式匹配指标行
        Pattern metricLinePattern = Pattern.compile("^(\\w+)(?:\\{([^}]*)\\})?\\s+([+-]?\\d+(\\.\\d+)?(?:[eE][+-]?\\d+)?)");


        Map<String, List<MetricSample>> metrics = new HashMap<>();

        for (String line : PROMETHEUS_TEXT.split("\\r?\\n")) {
            // 跳过注释和类型声明
            if (line.startsWith("#") || line.isEmpty()) {
                continue;
            }

            Matcher matcher = metricLinePattern.matcher(line);
            if (matcher.matches()) {
                String metricName = matcher.group(1);
                String labelsStr = matcher.group(2);
                String valueStr = matcher.group(3);

                // 解析标签（如果有）
                Map<String, String> labels = new HashMap<>();
                if (labelsStr != null && !labelsStr.isEmpty()) {
                    String[] labelPairs = labelsStr.split(",");
                    for (String labelPair : labelPairs) {
                        String[] keyValue = labelPair.split("=");
                        if (keyValue.length == 2) {
                            labels.put(keyValue[0].trim(), keyValue[1].trim());
                        }
                    }
                }

                // 创建 MetricSample 对象并添加到对应的指标列表中
                MetricSample sample = new MetricSample(metricName, Double.parseDouble(valueStr), labels);
                metrics.computeIfAbsent(metricName, k -> new ArrayList<>()).add(sample);
            }
        }

        // 打印解析结果
        for (Map.Entry<String, List<MetricSample>> entry : metrics.entrySet()) {
            System.out.println("Metric: " + entry.getKey());
            for (MetricSample sample : entry.getValue()) {
                System.out.println("  Value: " + sample.getValue() + ", Labels: " + sample.getLabels());
            }
        }
    }

    // 简单的 MetricSample 类来表示一个样本
    static class MetricSample {
        private final String metricName;
        private final double value;
        private final Map<String, String> labels;

        MetricSample(String metricName, double value, Map<String, String> labels) {
            this.metricName = metricName;
            this.value = value;
            this.labels = labels;
        }

        public String getMetricName() {
            return metricName;
        }

        public double getValue() {
            return value;
        }

        public Map<String, String> getLabels() {
            return labels;
        }

        @Override
        public String toString() {
            return "MetricSample{" +
                    "metricName='" + metricName + '\'' +
                    ", value=" + value +
                    ", labels=" + labels +
                    '}';
        }
    }

        public static void ss(String[] args) {
            String logLine = "INFO [http-nio-8080-exec-1] org.example.controller.RequestHandler : Received GET request for /api/users/123";
            String regex = "\\bGET\\s+(\\S+)"; // 匹配GET后面跟着的URL路径

            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(logLine);

            if (matcher.find()) {
                String path = matcher.group(1); // 获取匹配的第一组内容，即URL路径
                // 假设这里会有一个函数来增加Prometheus计数器或 gauge
                // incrementPrometheusMetric(path);
                System.out.println("Matched Path: " + path);
            }
        }
    }
